from django.core.management.base import BaseCommand, CommandError
from apps.order.models import *
from apps.message.models import *
from apps.product.models import *
from apps.repo.models import *
from datetime import timedelta

from django.db import transaction

class Command(BaseCommand):

    def handle(self, *args, **options):
        # 删除订单信息
        OrderPackage.objects.all().delete()
        SubOrderCommentPictures.objects.all().delete()
        SubOrderComment.objects.all().delete()
        Payment.objects.all().delete()
        SubOrder.objects.all().delete()
        Order.objects.all().delete()

        # # 删除消息
        PushRecord.objects.all().delete()
        UserUnreadMessage.objects.all().delete()
        UserHistoryMessage.objects.all().delete()
        Message.objects.all().delete()

        # # 删除产品信息
        GoodsBlock.objects.all().delete()
        UserCollectMarketOrder.objects.all().delete()
        MarketOrder.objects.all().delete()
        UserCollectProduct.objects.all().delete()
        UserAttentionProduct.objects.all().delete()
        UserAttentionBrand.objects.all().delete()
        ProductCommentPictures.objects.all().delete()
        ProductComment.objects.all().delete()
        GoodsBlockChain.objects.all().delete()

        # # 重置产品数据
        init_product_price = Decimal('0.03')
        zero_price = Decimal('0.00')
        init_publish_count = 50
        Product.objects.all().update(
            # stock=init_publish_count,
            status=Product.ProductStatus.REPO_IN_ING.name,
            pre_sale_price=init_product_price,
            # keeping_count=0,
            # trading_count=0,
            # picked_count=0,
            max_price=init_product_price,
            avg_price=init_product_price,
            last_trade_price=init_product_price,
            up_down_percent=zero_price,
            pre_sale_start_time=datetime.now(),
            pre_sale_end_time=datetime.now() + timedelta(minutes=30),
            publish_count=init_publish_count,
            init_store_unit_task_id='',
            in_repo_task_id='',
            dispatch_task_id='',
        )

        # # 重置仓库
        init_price_per_100g_1d = Decimal('0.01')
        StoreUnit.objects.all().delete()
        StoreUnitThree.objects.all().delete()
        Repo.objects.filter().update(price_per_100g_1d=init_price_per_100g_1d)

        # # 删除用户信息
        UserTunLevelRight.objects.all().delete()
        # UserTunLevelRightUseRecord.objects.all().delete()
        GrowCoinRecord.objects.all().delete()
        UserBalanceRecord.objects.all().delete()
        Address.objects.all().delete()
        Sign.objects.all().delete()
        AuthCode.objects.all().delete()
        WithdrawCash.objects.all().delete()
        UserTotalValue.objects.all().delete()
        UserIncome.objects.all().delete()
        # 重置用户信息
        User.objects.all().update(
            goods_block_count=0,
            trade_num=0,
            real_time_income=Decimal('0.00'),
            today_total_value_data={'points': []},
            current_week_total_value_data={'points': []},
            current_month_total_value_data={'points': []},
            balance=Decimal('0.00'),
            grow_coin=0,
            tun_level=TunLevel.objects.filter().order_by('need_grow_coin').first(),
            certificate_no='',
            certificate_state=User.CertificateState.UNVERIFIED.name,


        )
        # 添加充值记录
        for user in User.objects.all():
            UserBalanceRecord.objects.create(
                user=user,
                create_type=UserBalanceRecord.CreateType.RECHARGE.name,
                amount=user.balance,
                recharge_method=UserBalanceRecord.RechargeMethod.OFFLINE.name
            )
